<?php
namespace App\HttpController;


use App\Model\BaseModel;
use App\Toolys\LinkGatewayworker\Specification;
use EasySwoole\Http\AbstractInterface\Controller;
use GatewayClient\Gateway;
use http\Client;


class Index extends Controller
{

    public function index()
    {
        $this->response()->withStatus(404);
        $file = EASYSWOOLE_ROOT.'/Static/homePage/index.html';
        $this->response()->write(file_get_contents($file));
    }

    /** 测试片段
     * *    author 韩天尊
     */
    function test()
    {
        /*$data = _sealMysql(function($client){
            $model = BaseModel::invoke($client);
            return $model->tableName("test")->get(["id"=>1]);
        });
        var_dump($data->name);

        $redisData = _sealRedis(function (\EasySwoole\Redis\Redis $redis){
            $redis->set("key", "xxx");
            return $redis->get("key");
        });
        var_dump($redisData);*/


        $elasticsearch = elasticSearchInti();
        //插入
       /* go( function () use($elasticsearch){
            $bean = new \EasySwoole\ElasticSearch\RequestBean\Create();
            $bean->setIndex('user');
            $bean->setId(3);
            $bean->setBody([
                'name' => '张三3',
                'age' => 20,
                'hobby'=>'打球3、洗浴3、打架3'
            ]);
            $response = $elasticsearch->client()->create($bean)->getBody();
            var_dump(json_decode($response, true));
        });*/

        //修改  根据id修改
        /*$bean = new \EasySwoole\ElasticSearch\RequestBean\Update();
        $bean->setIndex('user');
        $bean->setId('2');
        $bean->setBody([
            'doc' => [
                'name'=>"李四",
                'hobby' => '哈哈、端端、呀呀呀、啦啦啦'
            ]
        ]);
        $response = $elasticsearch->client()->update($bean)->getBody();
        var_dump(json_decode($response, true));*/

        //批量修改暂时用不到  没学

        /*go(function () use ($elasticsearch) {
            $bean = new \EasySwoole\ElasticSearch\RequestBean\Delete();
            $bean->setIndex('my_index');
            $bean->setId('my_id');
            $response = $elasticsearch->client()->delete($bean)->getBody();
            $response = json_decode($response, true);
            var_dump($response);
        });*/

        /*$bean = new \EasySwoole\ElasticSearch\RequestBean\Get();
        $bean->setIndex('user');
        $bean->setId(2);
        $response = $elasticsearch->client()->get($bean)->getBody();
        var_dump(json_decode($response, true));*/

        //查询
        /*$bean = new \EasySwoole\ElasticSearch\RequestBean\Mget();
        $bean->setIndex('user');
        $bean->setBody(['ids' => [1,2,3]]);//查id
        $response = $elasticsearch->client()->mget($bean)->getBody();
        var_dump(json_decode($response, true));*/

        //查询
        /*$bean = new \EasySwoole\ElasticSearch\RequestBean\Search();
        $bean->setIndex('user');
        $bean->setBody([
            'query' => [
                //'multi_match'=>[]
                'match' => ['name' => '张三'],
                'match' => ['hobby' => '3'],
            ]
        ]);
        //$bean->setFrom(1);
        //$bean->setSize(1);
        $response = $elasticsearch->client()->search($bean)->getBody();
        var_dump(json_decode($response, true));*/

        //分页查询
        /*$sBean = new \EasySwoole\ElasticSearch\RequestBean\Search();
        $sBean->setIndex('user');
        $sBean->setScroll('1m');
        $sBean->setBody([
            'query' => [
                'match' => [
                    'hobby' => '球'
                ]
            ],
            'sort' => ['_doc'],
        ]);
        $sBean->setSize(1);
        $sResponse = $elasticsearch->client()->search($sBean)->getBody();
        $sResponse = json_decode($sResponse, true);
        $bean = new \EasySwoole\ElasticSearch\RequestBean\Scroll();
        $bean->setScrollId($sResponse['_scroll_id']);
        $bean->setScroll('1m');
        $response = $elasticsearch->client()->scroll($bean)->getBody();
        var_dump(json_decode($response, true));*/

        //统计
        /*$bean = new \EasySwoole\ElasticSearch\RequestBean\Count();
        $bean->setIndex("user");
        $bean->setBody([
            'query' => [
                'match' => [
                    'name' => '张三3'
                ]
            ]
        ]);
        $response = $elasticsearch->client()->count($bean)->getBody();
        var_dump(json_decode($response, true));*/
    }


    /** 拓展gateWayWorker
     * @author 韩天尊
     * @date 2022-03-23 11:10
     */
    public function linkGateWayWorker()
    {
        $data = json_decode($this->request()->getBody()->__toString());
        if (isset($data->action) && !empty($data->action)){
            go( function ()use($data){
                new Specification($data->action, $data->data, $data->_onMessageFromClientId);
            });
        }
        $this->response()->end();
    }


    protected function actionNotFound(?string $action)
    {
        switch ($this->request()->getMethod()){
            case "GET":{
                $this->response()->withStatus(404);
                $file = EASYSWOOLE_ROOT.'/vendor/easyswoole/easyswoole/src/Resource/Http/404.html';
                if(!is_file($file)){
                    $file = EASYSWOOLE_ROOT.'/src/Resource/Http/404.html';
                }
                $this->response()->write(file_get_contents($file));
                break;
            }
            default:{
                $this->writeJson(404, [], "啊喔！接口不存在");
            }
        }
    }
}